$(".grid-item").click(function() {
	var obj = $(this);
	var id = $(this).attr("ingId");
	var name = $(this).attr("ingName");
	var price = $(this).attr("ingPrice");
	var unit = $(this).attr("ingUnit");

	if (obj.hasClass('f_in')) {
		obj.removeClass("f_in");
		obj.addClass("f_in_sel");
		addPurchaseItem(id, name, 1, price, unit==null?"斤":unit);
	} else {
		obj.removeClass("f_in_sel");
		obj.addClass("f_in");
		removePurchaseItem(id);
	}
});

$(".addIngredient").click(function() {
	var obj = $(this).find("i");
	if (obj.hasClass('fa-chevron-down')) {
		obj.removeClass("fa-chevron-down");
		obj.addClass("fa-chevron-up");
		$(".inputIngWin").show();

	} else {
		obj.removeClass("fa-chevron-up");
		obj.addClass("fa-chevron-down");
		$(".inputIngWin").hide();

	}
});



function getITypeList() {
	$("#ingredientType").dsel("/itypelist", "", "暂无材料类型", true, function(len) {
		if (len > 0) {
			$("#delbtn").removeAttr("disabled");
		} else {
			$("#delbtn").attr("disabled", "disabled");
		}
	});
}

function filterIng() {
	var codition = "";
	var typeId = $("#ingredientType").val();
	var sx = $("#ingsx").val();
	if (sx != null && sx != "") {
		codition += "[sx*='" + sx + "']";
	}
	if (typeId != -1) {
		codition += "[typeid$='" + typeId + "']";
	}
	$(".grid-item").hide();
	$(".grid-item" + codition).show();
}

$('#ingsx').bind('input propertychange', function() {
	filterIng()
});
function addOtherItem() {
	var name = $("#ingaddname").val();
	var id = uuid();
	addPurchaseItem(id, name, 2, null, null);
}

function addPurchaseItem(id, name, type, gprice, gunit,count) {
	if (name == null || name == "") {
		alert("名字不能为空");
		return false;
	}
	var template = "<tr class='datatr' ingId='"
			+ id
			+ "' ingname='"
			+ name
			+ "' ingtype='"
			+ type
			+ "'> "
			+ " <td>"
			+ name
//			+ (type == 1 ? " (已有材料)" : "")
			+ "</td>"
			+ " <td><input type='number' style='width:50px' value='"+(count!=null?count:1)+"' class='sl'/><input type='text'  style='width:25px' value='"
			+ (gunit == null ? "斤" : gunit)
			+ "' class='unit'/></td>"
			+ " <td style='cursor:pointer' onclick='changeVal(this)' class='dj'>"
			+ gprice
			+ "</td>"
			+ " <td class='zj'>0.00</td>"
			+ " <td><i class='fa fa-minus-circle'  style='cursor:pointer;' onclick='removePurchaseItem(\""
			+ id + "\")'></i></td>" + " </tr>";
	var qobje = $(template);
	$("#cart").find(".counttr").before(qobje)
	priceSyn(qobje);

}

$("#cart").on('input propertychange', 'tr .sl', function() {
	priceSyn($(this).parent().parent());
});

$("#cart").on('blur', 'tr .dj', function() {
	priceSyn($(this).parent().parent());
});

function priceSyn(obj) {
	var p = /^[1-9](\d+(\.\d{1,4})?)?$/;
	var p1 = /^[0-9](\.\d{1,4})?$/;
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var dj = obj.find(".dj").html();
	var sl = obj.find(".sl").val();
	sl = reg.test(sl) ||sl==""? sl : 1;
	sl = sl <= 0&&sl!=""? 1 : sl;
	dj = (p1.test(dj) || p.test(dj)) ? dj : 0;
	zj = (dj * sl).toFixed(2);
	obj.find(".sl").val(sl);
	obj.find(".dj").val(dj);
	obj.find(".zj").html(zj);
	countTotal();
}

function savePurChase() {
	var remark = $("#remark").val();
	var buyItems = [];
	$("#cart .datatr").each(function() {
		var buyItem = {};
		var ingId = $(this).attr("ingId");
		var ingname = $(this).attr("ingname");
		var ingtype = $(this).attr("ingtype");
		var sl = $(this).find(".sl").val();
		var dj = $(this).find(".dj").html();
		var unit = $(this).find(".unit").val();
		buyItem["id"] = ingId;
		buyItem["name"] = ingname;
		buyItem["type"] = ingtype;
		buyItem["sl"] = sl;
		buyItem["dj"] = dj;
		buyItem["zj"] = (sl * dj).toFixed(2);
		buyItem["unit"] = unit;
		buyItems.push(buyItem);
	});
	$.post("/purchase/add", {
		"remark" : remark,
		"buyItems" : buyItems,
		"price" : $("#totalcount").html()
	}, function() {
		window.location.href = "/purchase/init";
	});
}

function changeVal(obj) {
	var cval = prompt("请输入单价", $(obj).html());
	if (cval != null) {
		$(obj).html(cval);
		priceSyn($(obj).parent());
	} else {
	}
}

function countTotal() {
	var count = 0;
	$(".zj").each(function() {
		count += parseFloat($(this).html());
	});
	$("#totalcount").html(count.toFixed(2));
}

function removePurchaseItem(id) {
	console.log($("#cart").find("tr[ingid='" + id + "']").size());
	$(".grid-item[ingid='" + id + "']").removeClass("f_in_sel");
	$(".grid-item[ingid='" + id + "']").addClass("f_in");
	$("#cart").find("tr[ingid='" + id + "']").remove();
	countTotal();
}

getITypeList();